package com.oracle.lingshangejia.fragment;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseFragment;
import com.oracle.lingshangejia.bean.RecordConsulationBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;

/*
 *  会诊记录S
 */
public class RecordOfConsultationsFragment extends BaseFragment implements View.OnClickListener {
    private int jmid;
    private int jzid;
    private TextView tv_yy;//会诊原因
    private TextView tv_yj;//会诊意见
    private TextView tv_hshow_doctor;//会诊医生
    private TextView tv_h_date;//会诊时间
    private ImageView iv_hui_img; //没有记录图片
    private View view_hz;
    private ScrollView scoll_hz;
    @Override
    protected void initData() {
        getNetWork();
    }

    private void getNetWork() {
        int ysid= (int) SpUtils.get(getActivity(),"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ysid",ysid);
        map.put("jzid",jzid);
        map.put("jmid",jmid);
        OkHttpUtils.getNetWork(getActivity(), Constant.baseurl+"online/jzzz/hzjlDetail",map,handler);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    if(s.contains("id")){  //有会诊记录
                        iv_hui_img.setVisibility(View.GONE);
                        view_hz.setVisibility(View.VISIBLE);
                        scoll_hz.setVisibility(View.VISIBLE);
                    }else{  //没有会诊记录
                        iv_hui_img.setVisibility(View.VISIBLE);
                        view_hz.setVisibility(View.GONE);
                        scoll_hz.setVisibility(View.GONE);
                    }
                    Gson gson=new Gson();
                    try{
                        RecordConsulationBean bean=gson.fromJson(s, RecordConsulationBean.class);
                        if(bean.getCode()==0){
                            tv_yy.setText(bean.getData().getHzyx());
                            tv_yj.setText(bean.getData().getHzyz());
                            String z="";
                            for(int i=0;i<bean.getYsList().size();i++){
                                z+=bean.getYsList().get(i)+",";
                            }
                            if(z.endsWith(",")){
                                z=z.substring(0,z.length()-1);
                            }
                            // tv_hshow_doctor.setText(z);
                            tv_h_date.setText(bean.getData().getHzrq());
                        }else{
                            Toast.makeText(getActivity(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getActivity(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getActivity(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    protected void initView() {
        jmid=getArguments().getInt("jmid");
        jzid=getArguments().getInt("jzid");
    }

    @Override
    protected void onBindViewBefore(View view) {
        tv_yy=view.findViewById(R.id.tv_yy);
        tv_yj=view.findViewById(R.id.tv_yj);
        // tv_hshow_doctor=view.findViewById(R.id.tv_hshow_doctor);
        tv_h_date=view.findViewById(R.id.tv_h_date);
        iv_hui_img=view.findViewById(R.id.iv_hui_img);
        view_hz=view.findViewById(R.id.view_hz);
        scoll_hz=view.findViewById(R.id.scoll_hz);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recordofconsultations;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }
}
